import {
    orderPage,
    cancel
} from '../../../api/myorder'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
    data: {
        pageLoading: false,
        status: '10',
        statusList: [{
                text: '全部',
                status: ''
            },
            {
                text: '代付款',
                status: '0'
            },
            {
                text: '待发货',
                status: '10'
            },
            {
                text: '待收货',
                status: '20'
            }, {
                text: '退款/售后',
                status: '100'
            }
        ],
        pageIndex: 1,
        refundStatus: false,
        orderList: [],
        total: 0,
        showState: false,
        content: '',
        orderId: '',
        type: ''
    },

    onShareAppMessage() {
        return {};
    },
    navToSearchPage() {

    },
    getOrderList(e) {
        const {
            status
        } = e.currentTarget.dataset;
        this.setData({
            status: status || '',
            pageIndex: 1,
            refundStatus: status == 100 ? true : false
        })
        this.getMyorder();
    },
    confirmDialog() {
        this.setData({
            showState: false
        })
        switch (this.data.type) {
            case '1':
                this.cancelOrder();
                break
        }
    },
    closeDialog() {
        this.setData({
            showState: false
        })
        this.setData({
            type: '',
            orderId: '',
            content: ''
        })
    },
    operateOrder(e) {
        const {
            id,
            type
        } = e.currentTarget.dataset;
        let content = ''
        if (type == 1) {
            content = '是否取消订单？'
        }
        this.setData({
            showState: true,
            content: content,
            type: type,
            orderId: id
        })
    },
    async cancelOrder() {
        this.setData({
            pageIndex: 1
        })
        const res = await cancel(this.data.orderId)
        this.getMyorder();
    },
    async getMyorder() {
        this.setData({
            pageLoading: true,
            orderList: []
        })
        const res = await orderPage(this.data.pageIndex, 10, this.data.status, this.data.refundStatus);
        res.list.map(item => {
            let delivery = ''
            let statusText = ''
            if (item.deliveryType == 1) {
                delivery = '快递'
            } else if (item.deliveryType == 2) {
                delivery = '自提'
            } else if (item.deliveryType == 31) {
                delivery = '配送'
            }
            if (item.refundStatus > 0) {
                if (item.refundStatus == 10) {
                    statusText = '申请中'
                } else if (item.refundStatus == 20) {
                    statusText = '卖家通过'
                } else if (item.refundStatus == 30) {
                    statusText = '卖家通过'
                } else if (item.refundStatus == 40) {
                    statusText = '等待平台退款'
                } else if (item.refundStatus == 50) {
                    statusText = '完成'
                } else if (item.refundStatus == 61) {
                    statusText = '买家取消售后'
                } else if (item.refundStatus == 62) {
                    statusText = '卖家拒绝'
                } else if (item.refundStatus == 63) {
                    statusText = '卖家拒绝收货'
                }
            } else {
                if (item.status == 0) {
                    statusText = '待付款'
                } else if (item.status == 10) {
                    statusText = '待发货'
                } else if (item.status == 20) {
                    statusText = '待发货'
                } else if (item.status == 30) {
                    statusText = '已完成'
                } else if (item.status == 40) {
                    statusText = '已取消'
                }
            }
            item.deliveryText = delivery
            item.statusText = statusText
            item.payPrice = (item.payPrice / 100).toFixed(2);
        })
        setTimeout(() => {
            this.setData({
                pageLoading: false,
                orderList: res.list,
                total: res.total
            })
        }, 100)

        console.log("订单详情", res);
    },
    jumpToDetail(e) {
        const {
            id
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/order/order-detail/index?id=' + id
        })
        //商品评价
        // wx.navigateTo({ 
        //     url: '/pages/goods/comments/create/index'
        // })

    },
    onLoad(query) {
        const {
            status
        } = query;
        this.setData({
            status: status || '',
            pageIndex: 1,
            refundStatus: status == 100 ? true : false
        })
        this.getMyorder();
    },
    onReachBottom() {
        console.log("潇洒");
    }
});